/**
 * (C) copyright lexio 2006
 * (C) copyright geolabs 2006-2007
 * author: Gérald Fenoy gerald [ at ] geolabs [ dot ] fr
 *
 * Controler class
 */
System.require("Ajax");

Controler=Class.create();

Controler.define({

  _init: function(){
      //alert("Controler contructor");
      return 1;
    },

      /**
       * Messages default values
       */
      readyStateMess: Array("Non initialisée;",
			    "Chargement en cours ...",
			    "Chargement terminé",
			    "Interactive",
			    "Terminé"),

      /**
       * Messages encapsulation
       */
      setReadyStateMess: function(n,value){ this.readyStateMess[n]=value; },

      setStatus: function(domel){ this.status=domel; },

      setCallback: function(){
      this.callback=arguments[0];
    },

      displayStatus: function(){
      //alert("Controler displayStatus");

      var message="";
      
      if(arguments.length==0){
	if(this.request){
	  message=this.readyStateMess[this.request.readyState];
	}
      }
      else
	message=arguments[0];
      if(this.status){
	try{
	  this.status.innerHTML=message;
	}
	catch(e){
	}
      }
    },

      execRequest: function(url,argv,options) {

      var options=options?options:{method: 'GET'};
      
      // clever but don't work on firefox
      if(this.request) this.request.abort();
      
      // setup request
      this.request=Ajax.request();
      
      if(this.request) {
	
	var tmp=Ajax.parseArg(argv);
	
	var rewritenUrl = url + (options.method!='POST'?"?"+tmp:"");
	try{
	  /**
	   * Setup request handler
	   */
	  this.timeStamp=new Date();
	  
	  this.request.onreadystatechange = this.execOnReqChange.bind(this);
	  
	  options.args=options.method!='POST'?null:tmp;
	  
	  Ajax.send(this.request,rewritenUrl,options);
	  
	  /**
	   * Call displayStatus request
	   */
	  this.displayStatus();
	}
	catch(e){
	  if(System.debug){
	    te="Chargement impossible : ["+this.datype+"] => "+e;
	    try{
	      document.getElementById(this.datype+'Status').innerHTML=te;
	    }
	    catch(e){;}
	  }
	}

      }
      else{
	if(System.debug){
	  te="Incapable d'envoyer la requete";
	  try{
	    document.getElementById(this.datype+'Status').innerHTML=te;
	  }
	  catch(e){
	    alert(te);
	  }
	}
      }

    },

      execOnReqChange: function(){
      
      if (this.request && this.request.readyState == 4 ) {
	try{
	  if (this.request.status == 200) {

	    if(!this.request.responseXML){
	      if(System.debug)
		alert("recreated the xml result from "+this.request.responseText);
	      this.request.responseXML=(new DOMParser()).parseFromString(this.request.responseText, "text/xml");
	    }

	    var xmldoc = this.request.responseXML;

	    //alert(xmldoc);
	    //alert("call newData"+this.request.responseXML+"\n"+this.request.responseText);
	    this.displayStatus();
	    this.newData();
	    if(this.callback){
	      //alert("run callback from "+this.name+"\n"+this.callback);
	      try{this.callback();}catch(e){alert(e);}
	      //alert("end callback");
	    }
	    //this.displayStatus();
	    //this.request=null;
	  }
	  else{
	    this.displayStatus("Error : "+this.request.status);
	  }
	}
	catch(e){
	  if(this && this.request)
	    this.request.abort();
	  message='Une erreur est survenue : '+e;
	  if(System.debug)
	    try{
	      this.displayStatus(message);
	    }
	    catch(e){
	      alert(message);
	    }
	  //alert(message);
	}
      }
      else {
	this.displayStatus();
      }

      if(this.request){
	var now=new Date();
	var poolp=new Date();
	poolp.setMinutes(this.timeStamp.getMinutes()+2,this.timeStamp.getSeconds()+55,this.timeStamp.getMilliseconds());
	if(now>poolp){
	  this.request.abort();
	  this.displayStatus('Temps d\'exécution dépassé : '+now+' '+poolp+'n');
	}
      }
	
    }

  });

BasicControler=Controler.extend();
BasicControler.define({
  newData: function(){
      this.display.innerHTML=this.request.responseText;
    }
  });

/**
 * StdControler class
 */
System.require("XmlToJson");
StdControler=Controler.extend();
StdControler.define({
  newData: function(){
      var tmp=new XmlToJson({xml:this.request.responseXML, root: "result", loop: "status"});
      tmp.createTree();
      this.tree=tmp.tree;
      this.request=null;
    }
  });


/**
 * MVCControler class
 */
MVControler=Controler.extend();
MVControler.define({
  _init: function(){
      this.limit=(arguments[0]&&arguments[0].limit)?arguments[0].limit:-1;
      this.tree={}; //JSON of the XML result
    },

  setView: function(view){
      this.view=view;
    },

  newData: function(){
      var tmp=new XmlToJson({xml:this.request.responseXML, root: "channel", loop: "item"});
      tmp.createTree();
      this.view.update(tmp);
    }

  });
